package com.example.high_slope_electronic_sand_table.common.util;

import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.List;

public class WeightRandomUtil {
    /**
     * 根据权重值生成随机数
     *  
     * @param firstLevel
     * @param secondLevel
     * @param thirdLevel
     * @param minValue
     * @param maxValue
     * @return
     */
    public static Float getWeightRandomValue(float firstLevel, float secondLevel, float thirdLevel, float minValue, float maxValue) {
        List<WeightRandom.WeightObj<Float>> weightObjs = new ArrayList<>();
        weightObjs.add(new WeightRandom.WeightObj<>(firstLevel, 1));
        weightObjs.add(new WeightRandom.WeightObj<>(secondLevel, 2));
        weightObjs.add(new WeightRandom.WeightObj<>(thirdLevel, 3));
        weightObjs.add(new WeightRandom.WeightObj<>(minValue, 40));
        Float next = RandomUtil.weightRandom(weightObjs).next();
        // 根据权重随机数的值再生成具体的区间随机数
        if (next.equals(firstLevel)) {
            return RandomUtil.randomFloat(firstLevel, maxValue);
        } else if (next.equals(secondLevel)) {
            return RandomUtil.randomFloat(secondLevel, firstLevel);
        } else if (next.equals(thirdLevel)) {
            return RandomUtil.randomFloat(thirdLevel, secondLevel);
        } else if (next.equals(minValue)) {
            return RandomUtil.randomFloat(minValue, thirdLevel);
        } else {
            return RandomUtil.randomFloat(minValue, thirdLevel);
        }
    }
}
